package io.dbw.modules.app.controller;


import java.util.HashMap;
import java.util.Map;

import io.dbw.common.utils.RedisUtils;
import io.dbw.common.utils.Result;
import io.dbw.modules.app.annotation.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.dbw.modules.app.form.LoginForm;
import io.dbw.modules.app.service.UserService;
import io.dbw.modules.app.utils.JwtUtils;
import io.dbw.modules.business.controller.sys.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * APP登录授权
 *
 *
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController extends AbstractController{
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public Result login(@RequestBody LoginForm form){

        //用户登录
        long userId = userService.login(form);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return writeSuccess(map);
    }
    /**
     * 登录
     */
    @PostMapping("getTest")
    @ApiOperation("登录")
    @Login
    public Result getTest(){
        return writeSuccess();
    }

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 登录
     */
    @GetMapping("test")
    @ApiOperation("登录")
    public Result test(){
        boolean set = redisUtils.set("33", "333");
        System.out.println(set);
        System.out.println(redisUtils.get("33"));
        return writeSuccess();
    }
}
